import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/9 9:43
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] grid = {{0,0,1,0,0}, {0,1,0,1,0}, {0,1,1,1,0}};
        solution.closedIsland(grid);
    }
    private int val;
    public int closedIsland(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int closedLandSum = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    val = 1;
                    dfs(grid, i, j);
                    closedLandSum += val;
                }
            }
        }
        return closedLandSum;
    }
    public void dfs(int[][] grid, int i, int j) {
        if (i < 0 || i == grid.length || j < 0 || j == grid[0].length) {
            val = 0;
            return;
        }
        if (grid[i][j] != 0) {
            return;
        }
        grid[i][j] = 1;
        dfs(grid, i - 1, j);
        dfs(grid, i  + 1, j);
        dfs(grid, i, j-1);
        dfs(grid, i, j +1);
    }
}
